package com.bitdf.txing.group.feign.impl.search;

import com.bitdf.txing.common.enume.TxCodeEnume;
import com.bitdf.txing.common.utils.R;
import com.bitdf.txing.group.service.GroupInfoService;
import com.bitdf.txing.search.feign.EsGroupFeignService;
import com.bitdf.txing.search.to.GroupInfoTo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Lizhiwei
 * @date 2023/9/6 19:59:27
 * 注释：
 */
@Service
@Slf4j
@ConditionalOnProperty(value = "tx.deploy.mode", havingValue = "standalone")
public class EsGroupFeignServiceImpl implements EsGroupFeignService {
    @Autowired
    GroupInfoService groupInfoService;
    @Override
    public R syncDatabase(List<GroupInfoTo> list) {
        List<com.bitdf.txing.group.to.GroupInfoTo> collect = list.stream().map(item -> {
            com.bitdf.txing.group.to.GroupInfoTo groupInfoTo = new com.bitdf.txing.group.to.GroupInfoTo();
            BeanUtils.copyProperties(item, groupInfoTo);
            return groupInfoTo;
        }).collect(Collectors.toList());
        boolean flag = false;
        try {
            flag = groupInfoService.syncDatabase(collect);
        } catch (Exception e) {
            log.info("{}", e);
        }
        return flag ? R.ok() : R.error(TxCodeEnume.COMMON_SYSTEM_UNKNOWN_EXCEPTION);
    }
}
